package com.i2bt.app.transmission.model;

import com.i2bt.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.widget.Button;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Transmission_item {
	private Button btu;
	private String fileName;
	private int fileSize;
	private int completeSize = 0;
	private int completeRate = 0;
	private boolean isComplete = false;
	private boolean isDownload = true;
	private String completeDate;
	private String downloadUrl;
	private String downloadPath;
	private int fileType = Transmission_item.FILE_TYPE_other;
	private Drawable drawable = null;
	private boolean state = true;

	public static final int FILE_TYPE_disk = 1;
	public static final int FILE_TYPE_folder = 2;
	public static final int FILE_TYPE_homebox = 3;
	public static final int FILE_TYPE_image = 4;
	public static final int FILE_TYPE_movie = 5;
	public static final int FILE_TYPE_music = 6;
	public static final int FILE_TYPE_other = 7;
	public static final int FILE_TYPE_txt = 8;

	public Transmission_item(String fileName, int fileSize, int completeSize,
			String downloadUrl, String downloadPath, int fileType, boolean state) {
		this.fileName = fileName;
		this.fileSize = fileSize;
		this.completeSize = completeSize;
		this.downloadUrl = downloadUrl;
		this.downloadPath = downloadPath;
		this.fileType = fileType;
		this.state = state;
	}

	public boolean isState() {
		return state;
	}

	public void setState(boolean state) {
		this.state = state;
	}

	public Transmission_item(String fileName, int fileSize, String downloadUrl,
			String downloadPath, int fileType, boolean state) {
		this(fileName, fileSize, 0, downloadUrl, downloadPath, fileType, state);
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public int getFileSize() {
		return fileSize;
	}

	public void setFileSize(int fileSize) {
		this.fileSize = fileSize;
	}

	public boolean isDownload() {
		return isDownload;
	}

	public void setDownload(boolean isDownload) {
		this.isDownload = isDownload;
	}

	public String getCompleteDate() {
		return completeDate;
	}

	public void setCompleteDate(String completeDate) {
		this.completeDate = completeDate;
	}

	public String getDownloadUrl() {
		return downloadUrl;
	}

	public void setDownloadUrl(String downloadUrl) {
		this.downloadUrl = downloadUrl;
	}

	public String getDownloadPath() {
		return downloadPath;
	}

	public void setDownloadPath(String downloadPath) {
		this.downloadPath = downloadPath;
	}

	public int getFileType() {
		return fileType;
	}

	public void setFileType(int fileType) {
		this.fileType = fileType;
	}

	public int getCompleteSize() {
		return this.completeSize;
	}

	public int getCompleteRate() {
		return (int) (((float) this.completeSize / this.fileSize) * 100);
	}

	public void setCompleteSize(int completeSize) {
		this.completeSize = completeSize;
		if (this.completeSize == this.fileSize) {
			this.isComplete = true;
			this.isDownload = false;
			this.completeRate = 100;
			SimpleDateFormat formatter = new SimpleDateFormat(
					"yyyy-MM-dd  HH:mm:ss");
			Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
			this.completeDate = formatter.format(curDate);
		}
	}

	public boolean isComplete() {
		if (this.fileSize == this.completeSize) {
			return true;
		} else {
			return false;
		}
	}

	public Drawable getDrawable(Context context) {
		int id = 0;
		if (this.fileType == Transmission_item.FILE_TYPE_disk) {
			id = R.drawable.files_icon_disk;
		} else if (this.fileType == Transmission_item.FILE_TYPE_folder) {
			id = R.drawable.files_icon_folder;
		} else if (this.fileType == Transmission_item.FILE_TYPE_homebox) {
			id = R.drawable.files_icon_homebox;
		} else if (this.fileType == Transmission_item.FILE_TYPE_image) {
			id = R.drawable.files_icon_image;
		} else if (this.fileType == Transmission_item.FILE_TYPE_movie) {
			id = R.drawable.files_icon_movie;
		} else if (this.fileType == Transmission_item.FILE_TYPE_music) {
			id = R.drawable.files_icon_music;
		} else if (this.fileType == Transmission_item.FILE_TYPE_txt) {
			id = R.drawable.files_icon_txt;
		} else {
			id = R.drawable.files_icon_other;
		}
		return context.getResources().getDrawable(id);
	}

	public Button getBtu() {
		return btu;
	}

	public void setBtu(Button btu) {
		this.btu = btu;
	}

	@SuppressLint("NewApi")
	public void startDownload(Context context) {
		isDownload = true;
		btu.setBackground(context.getResources().getDrawable(
				R.drawable.stop_download_selector));
	}

	@SuppressLint("NewApi")
	public void stopDownload(Context context) {
		isDownload = false;
		btu.setBackground(context.getResources().getDrawable(
				R.drawable.start_download_selector));
	}
}
